package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageRequestDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.service.WmChannelService;
import com.heima.wemedia.service.WmNewsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {


    @Override
    public ResponseResult findAll() {
        List<WmChannel> list = list();
        return ResponseResult.okResult(list);
    }

    /**
     * 增加频道
     *
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult saveNotRepeat(WmChannel wmChannel) {
        //参数校验
        if (wmChannel == null || StringUtils.isBlank(wmChannel.getName())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //数据处理
        //判断name是否重复
        if (extracted(wmChannel)) return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        //不重复,添加数据库
        save(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 分页查询频道
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult listPage(PageRequestDto dto) {
        //参数校验
        dto.checkParam();
        //执行分页
        IPage<WmChannel> page = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmChannel> lqw = new LambdaQueryWrapper<>();
        //模糊查询
        lqw.like(StringUtils.isNotBlank(dto.getName()), WmChannel::getName, dto.getName());
        //按创建时间倒序
        lqw.orderByDesc(WmChannel::getCreatedTime);
        page = page(page, lqw);
        ResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        pageResponseResult.setData(page.getRecords());
        return pageResponseResult;
    }


    @Autowired
    private WmNewsService wmNewsService;

    /**
     * 频道修改
     *
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult update(WmChannel wmChannel) {
        //参数校验
        if (wmChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //如果频道被引用,不能禁用
        //查询数据库channel的状态
        WmChannel dbChannel = getById(wmChannel.getId());
        //判断名称是否重复
        if (extracted(wmChannel)) return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);

        if (!wmChannel.getStatus().equals(dbChannel.getStatus())) {
            //修改状态,判断频道是否被引用
            List<WmNews> list = wmNewsService.list(Wrappers.<WmNews>lambdaQuery().eq(WmNews::getChannelId, wmChannel.getId()));
            if (list == null || list.size() == 0) {
                updateById(wmChannel);
                return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
            }
            //存在引用,不能被删除
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH, "频道存在引用,不能被禁用");
        } else {
            //不修改状态,可直接修改
            updateById(wmChannel);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
    }

    /**
     * 判断name是否重复
     *
     * @param wmChannel
     * @return
     */
    private boolean extracted(WmChannel wmChannel) {
        //获取本条数据的名称
        String name = wmChannel.getName();
        //数据库中查询该名称是否存在
        WmChannel wmChannel1 = getOne(Wrappers.<WmChannel>lambdaQuery().eq(WmChannel::getName, name));
        if (wmChannel1 != null && wmChannel1.getId() != wmChannel.getId()) {
            //存在该名称的频道,并且不是本次修改的频道;重复
            return true;
        }
        return false;
    }


    /**
     * 删除频道
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult del(Integer id) {
        //参数校验
        if (getById(id) == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //存在数据,判断是否禁用;没禁用不能删除
        if (getById(id).getStatus()) {
            //启用,不能删除
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH, "处于启用状态不能删除");
        }
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

/*    @Autowired
    private WmChannelMapper wmChannelMapper;
    @Override
    public ResponseResult getChannels() {
        return wmChannelMapper.;
    }*/
}